package com.company.indicationshots.adapter;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.company.indicationshots.R;

import java.util.List;

//通信设置下拉框数据加载适配器
public class CommSettingSpinnerAdapter extends ArrayAdapter<String> {

    private int selectItem = -1;

    public CommSettingSpinnerAdapter(@NonNull Context context, int resource, List<String> data) {
        super(context, resource,data);
    }

    @NonNull
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        View view = super.getView(position, convertView, parent);
        view.setPadding(5, 0, 0, 0);
        return view;
    }

    @Override
    public View getDropDownView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        // 如果convertView为null，则通过LayoutInflater来创建新的视图
        if (convertView == null) {
            LayoutInflater inflater = LayoutInflater.from(getContext());
            convertView = inflater.inflate(R.layout.comm_spinner_item_layout, parent, false);
        }

        // 获取TextView实例
        TextView spinnerItem = convertView.findViewById(R.id.spinner_item); // 假设这是你的TextView的ID

        // 设置TextView的文本内容
        spinnerItem.setText(getItem(position));

        // 检查当前项是否被选中
        if (position == selectItem) {
            // 如果被选中，设置背景为黄色
            convertView.setBackgroundColor(Color.parseColor("#FFA500"));
        } else {
            convertView.setBackgroundColor(Color.parseColor("#40D3D3D3"));
        }

        // 返回修改后的视图
        return convertView;
    }

    public void setSelectItem(int position){
        this.selectItem = position;
        notifyDataSetChanged();
    }

}
